

/******************************************************************************/
/* Bank Level Analysis                                                        */
/******************************************************************************/



use "$LocalData\BankLevelAnalysis.dta", clear
drop cluster3

qui tab year, gen(year_)

*Deposits by Deciles
noi display "Deposit by Deciles regressions."

gen temp = 1 if small==1 & year==2002
bysort cert: egen small2002 = max(temp)
drop temp

sort cert year
merge m:1 cert using "$LocalData\BankCluster3_2002.dta"
keep if _merge==3
* Restrict to banks with deposit growth positive or negative 500% or less


local Filters year>2002 & abs(bank_lndep)<=5 & SBquant2002!=. & small2002==1 &  Lsmall==1 & merger==0
eststo: xtivreg2 bank_lndep (bank_gSmallFirm1 = bank_bartikGS1 )  year_* if abs(bank_lndep)<5 & year>2002 & Lsmall==1  & `Filters', fe cluster(cert)
gen mainsample = e(sample)
local Filters mainsample==1
bysort cert: egen singlecert = sum(mainsample)
local Filters mainsample==1 & singlecert>1 

eststo: areg bank_lndep bank_gSmallFirm1  year_* if abs(bank_lndep)<5  & Lsmall==1 & year>2002 & `Filters', vce(cluster cluster3) absorb(cert) 
outreg2 using "$LocalData\Tables\BankLevelReg_RFS", tex replace ///
	ctitle(DepGr) keep(bank_gSmallFirm1 bank_gLargeFirm1) ///
	addtext(REG, OLS, YEAR FE, YES, Bank FE, YES, YRS, 2003-2017)

eststo: areg bank_lndep bank_gSmallFirm1 bank_gLargeFirm1  year_* if abs(bank_lndep)<5  & Lsmall==1 & year>2002 & `Filters', vce(cluster cluster3) absorb(cert) 
outreg2 using "$LocalData\Tables\BankLevelReg_RFS", tex append ///
	ctitle(DepGr) keep(bank_gSmallFirm1 bank_gLargeFirm1) ///
	addtext(REG, OLS, YEAR FE, YES, Bank FE, YES, YRS, 2003-2017)

eststo: areg bank_gSmallFirm1 bank_bartikGS1  year_* if abs(bank_lndep)<5  & Lsmall==1 & year>2002 & `Filters', vce(cluster cluster3) absorb(cert) 
outreg2 using "$LocalData\Tables\BankLevelReg_RFS", tex append ///
	ctitle(DepGr) keep(bank_gSmallFirm1 bank_gLargeFirm1 bank_bartikGS1) ///
	addtext(REG, OLS, YEAR FE, YES, Bank FE, YES, YRS, 2003-2017)

	eststo: xtivreg2 bank_lndep (bank_gSmallFirm1 = bank_bartikGS1 )  year_* if abs(bank_lndep)<5 & year>2002 & Lsmall==1  & `Filters', fe cluster(cluster3)
	local widstat = round(`e(widstat)',0.1)	
outreg2 using "$LocalData\Tables\BankLevelReg_RFS", tex append ///
	ctitle(DepGr) keep(bank_gSmallFirm1 bank_gLargeFirm1) ///
	addtext(REG, 2SLS, F-stat, `widstat', YEAR FE, YES, Bank FE, YES, YRS, 2003-2017)	
	
local Filters year>2002 & abs(bank_lndep)<=5 & SBquant2002!=. & small2002==1 &  Lsmall==1 & merger==0
eststo: areg bank_lndep bank_gSmallFirm1  year_* if abs(bank_lndep)<5  & Lsmall==1 & year>2002 & `Filters' & sbl2002<5, vce(cluster cluster3) absorb(cert) 
outreg2 using "$LocalData\Tables\BankLevelReg_RFS", tex append ///
	ctitle(DepGr) keep(bank_gSmallFirm1 bank_gLargeFirm1) ///
	addtext(REG, OLS, YEAR FE, YES, Bank FE, YES, YRS, 2003-2017)

eststo: areg bank_lndep bank_gSmallFirm1 bank_gLargeFirm1  year_* if abs(bank_lndep)<5  & Lsmall==1 & year>2002 & `Filters'  & sbl2002<5, vce(cluster cluster3) absorb(cert) 
outreg2 using "$LocalData\Tables\BankLevelReg_RFS", tex append ///
	ctitle(DepGr) keep(bank_gSmallFirm1 bank_gLargeFirm1) ///
	addtext(REG, OLS, YEAR FE, YES, Bank FE, YES, YRS, 2003-2017)


local Filters year>2002 & abs(bank_lndep)<=5 & SBquant2002!=. & small2002==1 &  Lsmall==1 & merger==0

eststo: xtivreg2 bank_lndep (bank_gSmallFirm1 = bank_bartikGS1 )  year_* if abs(bank_lndep)<5 & year>2002 & Lsmall==1  & `Filters'  & sbl2002>5, fe cluster(cluster3)
gen mainsb = e(sample)
bysort cert: egen singlesb = sum(mainsb)

local Filters mainsb==1 & singlesb>1 
	
	
eststo: areg bank_lndep bank_gSmallFirm1  year_* if abs(bank_lndep)<5  & Lsmall==1 & year>2002 & `Filters' & sbl2002>5, vce(cluster cluster3) absorb(cert) 
outreg2 using "$LocalData\Tables\BankLevelReg_RFS", tex append ///
	ctitle(DepGr) keep(bank_gSmallFirm1 bank_gLargeFirm1) ///
	addtext(REG, OLS, YEAR FE, YES, Bank FE, YES, YRS, 2003-2017)

eststo: areg bank_lndep bank_gSmallFirm1 bank_gLargeFirm1  year_* if abs(bank_lndep)<5  & Lsmall==1 & year>2002 & `Filters'  & sbl2002>5, vce(cluster cluster3) absorb(cert) 
outreg2 using "$LocalData\Tables\BankLevelReg_RFS", tex append ///
	ctitle(DepGr) keep(bank_gSmallFirm1 bank_gLargeFirm1) ///
	addtext(REG, OLS, YEAR FE, YES, Bank FE, YES, YRS, 2003-2017)

eststo: xtivreg2 bank_lndep (bank_gSmallFirm1 = bank_bartikGS1 )  year_* if abs(bank_lndep)<5 & year>2002 & Lsmall==1  & `Filters'  & sbl2002>5, fe cluster(cluster3)
	local widstat = round(`e(widstat)',0.1)	
outreg2 using "$LocalData\Tables\BankLevelReg_RFS", tex append ///
	ctitle(DepGr) keep(bank_gSmallFirm1 bank_gLargeFirm1) ///
	addtext(REG, 2SLS, F-stat, `widstat', YEAR FE, YES, Bank FE, YES, YRS, 2003-2017)	
	

	
	
********************************************************
********************************************************
********************************************************
********************************************************
********************************************************


bysort year: egen bank_prov99 = pctile(bank_prov), p(99)
bysort year: egen bank_prov01 = pctile(bank_prov), p(1)

bysort year: egen roa99 = pctile(roa), p(99)
bysort year: egen roa01 = pctile(roa), p(1)

bysort year: egen bank_lnci99 = pctile(bank_lnci), p(99)
bysort year: egen bank_lnci01 = pctile(bank_lnci), p(1)

bysort year: egen bank_lnre99 = pctile(bank_lnre), p(99)
bysort year: egen bank_lnre01 = pctile(bank_lnre), p(1)

local Filters year>2002 & abs(bank_lndep)<=5 & small2002==1 &  Lsmall==1 & merger==0
eststo: xtivreg2 roa (bank_gSmallFirm1 = bank_bartikGS1 )  year_* if roa<roa99 & roa>roa01  & year>2002 & Lsmall==1  & `Filters' , fe cluster(cluster3)
gen roasample = e(sample)
local Filters roasample==1
bysort cert: egen roa_single = sum(roasample)
local Filters roasample==1 & roa_single>1

eststo: areg roa bank_gSmallFirm1  year_* if roa<roa99 & roa>roa01  & Lsmall==1 & year>2002 & `Filters', vce(cluster cluster3) absorb(cert) 
outreg2 using "$LocalData\Tables\BankLevelReg_RFS2", tex replace ///
	ctitle(ROA) keep(bank_gSmallFirm1 bank_gLargeFirm1) ///
	addtext(REG, OLS, YEAR FE, YES, Bank FE, YES, YRS, 2003-2017)

eststo: xtivreg2 roa (bank_gSmallFirm1 = bank_bartikGS1 )  year_* if roa<roa99 & roa>roa01  & year>2002 & Lsmall==1  & `Filters' , fe cluster(cluster3)
outreg2 using "$LocalData\Tables\BankLevelReg_RFS2", tex append ///
	ctitle(ROA) keep(bank_gSmallFirm1 bank_gLargeFirm1) ///
	addtext(REG, 2SLS, YEAR FE, YES, Bank FE, YES, YRS, 2003-2017)

local Filters year>2002 & abs(bank_lndep)<=5 & small2002==1 &  Lsmall==1 & merger==0
eststo: xtivreg2 bank_prov (bank_gSmallFirm1 = bank_bartikGS1 )  year_* if bank_prov<bank_prov99 & bank_prov>bank_prov01  & year>2002 & Lsmall==1  & `Filters' , fe cluster(cluster3)
gen provsample = e(sample)
local Filters provsample==1
bysort cert: egen prov_single = sum(provsample)
local Filters provsample==1 & prov_single>1

eststo: areg bank_prov bank_gSmallFirm1  year_* if bank_prov<bank_prov99 & bank_prov>bank_prov01 & Lsmall==1 & year>2002 & `Filters', vce(cluster cluster3) absorb(cert) 
outreg2 using "$LocalData\Tables\BankLevelReg_RFS2", tex append ///
	ctitle(Prov) keep(bank_gSmallFirm1 bank_gLargeFirm1) ///
	addtext(REG, OLS, YEAR FE, YES, Bank FE, YES, YRS, 2003-2017)

	eststo: xtivreg2 bank_prov (bank_gSmallFirm1 = bank_bartikGS1 )  year_* if bank_prov<bank_prov99 & bank_prov>bank_prov01 & year>2002 & Lsmall==1  & `Filters' , fe cluster(cluster3)
outreg2 using "$LocalData\Tables\BankLevelReg_RFS2", tex append ///
	ctitle(Prov) keep(bank_gSmallFirm1 bank_gLargeFirm1) ///
	addtext(REG, 2SLS, YEAR FE, YES, Bank FE, YES, YRS, 2003-2017)

local Filters year>2002 & abs(bank_lndep)<=5 & small2002==1 &  Lsmall==1 & merger==0
eststo: xtivreg2 bank_lnci (bank_gSmallFirm1 = bank_bartikGS1 )  year_* if bank_lnci<bank_lnci99 & bank_lnci>bank_lnci01  & year>2002 & Lsmall==1  & `Filters' , fe cluster(cluster3)
gen cisample = e(sample)
local Filters cisample==1
bysort cert: egen ci_single = sum(cisample)
local Filters cisample==1 & ci_single>1

eststo: areg bank_lnci bank_gSmallFirm1  year_* if bank_lnci<bank_lnci99 & bank_lnci>bank_lnci01  & Lsmall==1 & year>2002 & `Filters', vce(cluster cluster3) absorb(cert) 
outreg2 using "$LocalData\Tables\BankLevelReg_RFS2", tex append ///
	ctitle(CIgr) keep(bank_gSmallFirm1 bank_gLargeFirm1) ///
	addtext(REG, OLS, YEAR FE, YES, Bank FE, YES, YRS, 2003-2017)


eststo: xtivreg2 bank_lnci (bank_gSmallFirm1 = bank_bartikGS1 )  year_* if bank_lnci<bank_lnci99 & bank_lnci>bank_lnci01  & year>2002 & Lsmall==1  & `Filters' , fe cluster(cluster3)
outreg2 using "$LocalData\Tables\BankLevelReg_RFS2", tex append ///
	ctitle(ROA) keep(bank_gSmallFirm1 bank_gLargeFirm1) ///
	addtext(REG, 2SLS, YEAR FE, YES, Bank FE, YES, YRS, 2003-2017)

local Filters year>2002 & abs(bank_lndep)<=5 & small2002==1 &  Lsmall==1 & merger==0	
eststo: xtivreg2 bank_lnre (bank_gSmallFirm1 = bank_bartikGS1 )  year_* if bank_lnre<bank_lnre99 & bank_lnre>bank_lnre01  & year>2002 & Lsmall==1  & `Filters' , fe cluster(cluster3)
gen resample = e(sample)
local Filters resample==1
bysort cert: egen re_single = sum(resample)
local Filters resample==1 & re_single>1

eststo: areg bank_lnre bank_gSmallFirm1  year_* if bank_lnre<bank_lnre99 & bank_lnre>bank_lnre01  & Lsmall==1 & year>2002 & `Filters', vce(cluster cluster3) absorb(cert) 
outreg2 using "$LocalData\Tables\BankLevelReg_RFS2", tex append ///
	ctitle(ReGr) keep(bank_gSmallFirm1 bank_gLargeFirm1) ///
	addtext(REG, OLS, YEAR FE, YES, Bank FE, YES, YRS, 2003-2017)

eststo: xtivreg2 bank_lnre (bank_gSmallFirm1 = bank_bartikGS1 )  year_* if bank_lnre<bank_lnre99 & bank_lnre>bank_lnre01 & year>2002 & Lsmall==1  & `Filters' , fe cluster(cluster3)
outreg2 using "$LocalData\Tables\BankLevelReg_RFS2", tex append ///
	ctitle(ReGr) keep(bank_gSmallFirm1 bank_gLargeFirm1) ///
	addtext(REG, 2SLS, YEAR FE, YES, Bank FE, YES, YRS, 2003-2017)
	
		
	


noi display "Finished running Deposit by Deciles regressions."
*noi etime
noi display " "

******************************************************************************

